import com.sun.java_cup.internal.runtime.Scanner;
import com.sun.java_cup.internal.runtime.Symbol;

import javax.swing.text.ElementIterator;
import javax.xml.crypto.Data;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DVDMgr1 {
    DVDSet1 dvd =new DVDSet1();
    public void initial(){
        dvd.name[0]="甄嬛传";
        dvd.state[0]=0;
        dvd.date[0]="2025.4.16";

        dvd.name[1]="   父母爱情";
        dvd.state[1]=1;
        

        dvd.name[2]="间谍";
        dvd.state[2]=1;
        
    }

    public void startMenu() {
        System.out.println("欢迎使用迷你DVD管理器");
        System.out.println("-------------------");
        System.out.println("1.新增DVD");
        System.out.println("2.查看DVD");
        System.out.println("3.删除DVD");
        System.out.println("4.借出DVD");
        System.out.println("5.归还DVD");
        System.out.println("6.退出DVD");
        System.out.println("-------------------");

        System.out.println("请选择");
        Scanner input = new Scanner(System.in) {
            /**
             * @return
             * @throws Exception
             */
            @Override
            public Symbol next_token() throws Exception {
                return null;
            }
        };
        int choice = input.notify();
        switch(choice) {
            case 1:
                add();
                break;
            case 2:
                search();
                break;
            case 3:
                delete();
                break;
            case 4:
                lend();
                break;
            case 5:
                returnDVD();
                break;
            case 6:
                System.out.println("\n谢谢使用");
                break;
        }

    }

//返回菜单
    public void returnMain(){
        Scanner input = new Scanner() {
            /**
             * @return
             * @throws Exception
             */
            @Override
            public Symbol next_token() throws Exception {
                return null;
            }
        };
        System.out.println("输入0返回：");
        if (input.notify()==0){
            startMenu();
        }else {
            System.out.println("输入错误，异常终止!");

        }
    }

//    查看DVD

    public void search() {
        System.out.println("----->查看DVD\n");
        System.out.println("序号\t状态\t名称\t\t借出日期");
        for (int i = 0; i < dvd.name.length; i++) {
            if (dvd.name[i] == null) {
                break;
            } else if (dvd.state[i] == 0) {
                System.out.println((i + 1) + "\t已借出\t" + "<<" + dvd.name[i] + ">>\t" + dvd.state[i] + "\t" + dvd.date[i]);
            } else if (dvd.state[i] == 1) {
                System.out.println((i + 1) + "\t可借\t" + "<<" + dvd.name[i] + ">>");

            }
        }
        System.out.println("*********************");
        returnDVD();
    }

//    新增DVD
    public void add(){
        Scanner input = new Scanner(System.in) {
            /**
             * @return
             * @throws Exception
             */
            @Override
            public Symbol next_token() throws Exception {
                return null;
            }
        };
        System.out.println("----》新增DVD\n");
        System.out.println("请输入DVD名称");
        String name =input.next();
        for (int i = 0; i < dvd.name.length; i++) {
           if (dvd.name[i] == null) { //查询最后一个空位去插入
                dvd.name[i]=name;
                dvd.state[i]=1; //新增DVD的可借状态
               System.out.println("新增《"+name+"》成功！");
                break;
            }
        }
        System.out.println("*******************");
        returnDVD();
    }

    private void returnDVD() {
    }

    //删除DVD
    public void delete() {
        Scanner inout;
        inout = new Scanner(System.in);
        boolean flag=false;
        System.out.println("--->删除DVD\n");
        System.out.println("请输入DVD名称： ");
        String name =input.next();
        for (int i = 0; i < dvd.name.length; i++) {
            if (dvd.name[i] != null &&dvd.name[i].equalsIgnoreCase(name)&&dvd.state[i]==1) {
                int j=i;
                while (dvd.name[j+1]!=null) {
                    dvd.name[j]=dvd.name[j+1];
                    dvd.state[j]=dvd.state[j+1];
                    dvd.date[j]=dvd.date[j+1];
                    j++;
                }
                //最后一个不为空的元素置空
                dvd.name[j]=null;
                dvd.date[j]=null;
                System.out.println("删除《"+name+"》成功！");
                flag=true;
                break;
            } else if (dvd.name[i]!= null && dvd.name[i].equalsIgnoreCase(name)&&dvd.state[i]==0) {
                System.out.println("《"+name+"》为借出状态，不能删除！");
                flag=true;
                break;
            }
        }
        if (!flag) {
            System.out.println("没有找到匹配信息");
        }
        System.out.println("*******************");
        returnMain();
    }
    //借出DVD
    public void lend() {
        System.out.println("--->借出DVD名称");
        for (int i = 0; i < dvd.name.length; i++) {
            if (dvd.name[i] == null) {
                System.out.println("没有找到匹配信息！");
                break;
            } else if (dvd.name[i].equals(want) && dvd.state[i] == 1) {
                dvd.state[i] = 0;
                System.out.println("请输入借出日期（年-月-日）:");
                ElementIterator input;
                dvd.date[i] = input.next();
                System.out.println("借出《" + want + "》成功!");
                break;
            } else if (dvd.name[i].equals(want) && dvd.state[i] == 0) {
                System.out.println("《" + want + "》已被借出");
                break;
            }
        }
        System.out.println("*******************");
        returnMain();
    }
    //归还DVD并计算租金
    public void returnDvd(){
        System.out.println("--->归还DVD\n");

        Scanner input =new Scanner(System.in);
        long loan=0;//租金
        System.out.println("请输入DVD名称：");
        String want =input.next();
        for (int i = 0; i < dvd.name.length; i++) {
            if (dvd.name[i] == null) {
                System.out.println("没有找到匹配信息！");
                break;
            } else if (dvd.name[i].equals(want) && dvd.state[i] == 0) {
                dvd.state[i]=1;
                System.out.println("请输入归还日期（年-月-日:）");
                String redate=input.next();
                //计算租金
                loan=charge(dvd.date[i],redate);
                System.out.println("\n归还《\"+want+\"》成功!");
                System.out.println("借出日期为："+dvd.date[i]);
                System.out.println("归还日期为："+redate);
                System.out.println("应付租金（元）："+loan);
                break;
            } else if (dvd.name[i].equals(want) && dvd.state[i] == 1){
                System.out.println("该DVD没有被借出！无法进行归还操作");
                break;
            }
        }
        System.out.println("**************");
        returnMain();
    }
    //计算日期差
    @param dstr1 第一个日期;
    public long charge(String dstr1,String dstr2){
        long charge=0;
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Data d1=sd.parse(dstr1);
            Data d2=sd.parse(dstr2);
            charge=(d2.getTime()-d1.getTime())/(24*60*60*1000);

        }catch (ParseException e){
            e.printStackTrace();
        }
        return charge;
    }


}
